package beanstao.util.json;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsDateJsonBeanProcessor;

import beanstao.util.Strings;

import com.google.common.collect.Lists;

public class JsonLib
{
	public static String fromObject(Object obj)
	{
		return fromObject(obj, "");
	}

	@SuppressWarnings("rawtypes")
	public static String fromObject(Object obj, String configName)
	{
		JsonConfig jsonConfig = getConfig();// jsonConfigBuilder(obj.getClass(), JsonLibConfig.builder(configName));
		if (obj instanceof String)
			if (obj.toString().trim().charAt(0) == '{')
				if (jsonConfig == null)
					return JSONObject.fromObject(obj).toString();
				else
					return JSONObject.fromObject(obj, jsonConfig).toString();
			else if (obj.toString().trim().charAt(0) == '[')
				if (jsonConfig == null)
					return JSONArray.fromObject(obj).toString();
				else
					return JSONArray.fromObject(obj, jsonConfig).toString();
			else
				return obj.toString();

		Class c = obj.getClass();
		if ((obj instanceof List) || (obj instanceof Collection) || (obj instanceof Set) || (c.isArray()))
			if (jsonConfig == null)
				return JSONArray.fromObject(obj).toString();
			else
				return JSONArray.fromObject(obj, jsonConfig).toString();
		if (jsonConfig == null)
			return JSONObject.fromObject(obj).toString();
		else
			return JSONObject.fromObject(obj, jsonConfig).toString();
	}

	public static ArrayList<String> toObject(String jsonString)
	{
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		ArrayList<String> list = new ArrayList<String>(jsonArray.size());
		// 得到这个jsonArray后要转化为ArrayList，需要用循环遍历
		for (int i = 0; i < jsonArray.size(); i++)
		{
			Object o = jsonArray.get(i);
			JSONObject jsonObject = JSONObject.fromObject(o);
			String Person = (String) JSONObject.toBean(jsonObject, String.class);
			list.add(Person);
		}
		return list;
	}

	private static JsonConfig config = null;

	private static JsonConfig getConfig()
	{
		if (config == null)
		{
			config = new JsonConfig();
			config.registerJsonValueProcessor("getTicketTime".toLowerCase(), new JsonDateValueProcessor());
			config.registerJsonValueProcessor("callCustomerTime".toLowerCase(), new JsonDateValueProcessor());
			config.registerJsonValueProcessor("operationBeginTime".toLowerCase(), new JsonDateValueProcessor());
			config.registerJsonValueProcessor("operationEndTime".toLowerCase(), new JsonDateValueProcessor());
			config.registerJsonValueProcessor("askEvaluationTime".toLowerCase(), new JsonDateValueProcessor());
			config.registerJsonValueProcessor("createdTime".toLowerCase(), new JsonDateValueProcessor());
			config.registerJsonValueProcessor("modifedTime".toLowerCase(), new JsonDateValueProcessor());
		}
		return config;
	}

	@SuppressWarnings({ "rawtypes" })
	public static JsonConfig jsonConfigBuilder(Class cls, final JsonLibConfig config)
	{
		if (config == null)
			return null;

		// 设置要排除的属性
		JsonConfig jsonConfig = new JsonConfig();
		// jsonConfig.setExcludes(config.getExcludeNames());
		// // jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);
		//
		// // 设置改名输出
		// PropertyNameProcessor propertyNameProcessor = new PropertyNameProcessor()
		// {
		// @Override
		// public String processPropertyName(Class arg0, String arg1)
		// {
		// if (config.getRenameMap().containsKey(arg1))
		// {
		// return config.getRenameMap().get(arg1);
		// }
		// return arg1;
		// }
		// };
		// jsonConfig.registerJsonPropertyNameProcessor(cls, propertyNameProcessor);

		// 当输出时间格式时，采用和JS兼容的格式输出
		jsonConfig.registerJsonBeanProcessor(java.util.Date.class, new JsDateJsonBeanProcessor());// new
		// JsonDateValueProcessor());
		// jsonConfig.registerJsonBeanProcessor(EnableEventCollection.class, new EnableCollectionJsonBeanProcessor(config));

		return jsonConfig;
	}

	/**
	 * 将一个数组的Json表达字符串转换成一个ArrayList
	 * 
	 * @param object 数组的Json表达字符串
	 */
	public static ArrayList<String> fromStringArray(Object object)
	{
		ArrayList<String> list;
		if (!Strings.isQuoteBy(object.toString().trim(), '[', ']'))
			return Lists.newArrayList();
		JSONArray array = JSONArray.fromObject(object);
		list = new ArrayList<String>(array.size());
		for (int i = 0; i < array.size(); i++)
			list.add(String.valueOf(array.get(i)));
		return list;
	}
}
